using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationInvoiceSumup
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private object missing = Missing.Value;

		private string _sStartupPath = null;

		public ExcelOperationInvoiceSumup(string sStartupPath)
		{
			this._sStartupPath = sStartupPath;
			if (this._sStartupPath.Substring(this._sStartupPath.Length - 1, 1) != "\\")
			{
				this._sStartupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeSumupDataToExcel(string templetFile, System.Data.DataTable dtSumUp)
		{
			this.OpenExcel(templetFile);
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("invoiceSumup");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeSumupDataToExcel_1(dtSumUp);
			}
		}

		private void MakeSumupDataToExcel_1(System.Data.DataTable dtSumUp)
		{
			int num = 0;
			decimal num2 = 0m;
			decimal num3 = 0m;
			int num4 = 0;
			try
			{
				for (int i = 0; i < dtSumUp.Rows.Count; i++)
				{
					this.workSheet.Cells[4 + num4, 1]= dtSumUp.Rows[i]["发货单号"];
					this.workSheet.Cells[4 + num4, 2]= dtSumUp.Rows[i]["客户"];
					this.workSheet.Cells[4 + num4, 3]= dtSumUp.Rows[i]["计划"];
					this.workSheet.Cells[4 + num4, 4]= dtSumUp.Rows[i]["订货日期"].ToString();
					this.workSheet.Cells[4 + num4, 5]= DateTime.Parse(dtSumUp.Rows[i]["发货日期"].ToString()).ToShortDateString();
					this.workSheet.Cells[4 + num4, 6]= dtSumUp.Rows[i]["数量"];
					this.workSheet.Cells[4 + num4, 7]= dtSumUp.Rows[i]["面积"];
					this.workSheet.Cells[4 + num4, 8]= dtSumUp.Rows[i]["金额"];
					this.workSheet.Cells[4 + num4, 9]= dtSumUp.Rows[i]["类型"];
					this.workSheet.Cells[4 + num4, 10]= dtSumUp.Rows[i]["备注"];
					num += Convert.ToInt32(dtSumUp.Rows[i]["数量"]);
					num2 += Convert.ToDecimal(dtSumUp.Rows[i]["面积"]);
					num3 += Convert.ToDecimal(dtSumUp.Rows[i]["金额"]);
					num4++;
				}
				this.workSheet.Cells[4 + num4, 4]= "总计";
				this.workSheet.Cells[4 + num4, 6]= num;
				this.workSheet.Cells[4 + num4, 7]= num2;
				this.workSheet.Cells[4 + num4, 8]= num3;
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
